package com.hyving.summer.picmatch;

import com.hyving.summer.picmatch.strategy.FingerPrintStrategy;
import lombok.SneakyThrows;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PicMatchMain {


    public static void main(String[] args) {


        getFile();


    }



    @SneakyThrows
    public static void getFile(){
        String path="C:\\Users\\Administrator\\Desktop\\灰色防晒衣\\灰色防晒衣";
        File folder =new File(path);
        if(!folder.isDirectory()){
            System.out.println("错误");
        }
        Map<String,byte[]> fingerMap =new HashMap<>();
        List<String> fileList =new ArrayList<>();
        File[] files = folder.listFiles();
        if (files != null) {
            for (File file : files) {
                if (file.isFile()) {
                    BufferedImage image =ImageIO.read(file);
                    byte[] bytes =FingerPrintStrategy.hashValue(image);
                    fingerMap.put(file.getName(),bytes);
                    fileList.add(file.getName());
                }
            }
        }

        for(int i=0;i<fileList.size()-1;i++){
            String fileName =fileList.get(i);

            for(int j =i+1;j<fileList.size();j++){

                String comName =fileList.get(j);
                float f =FingerPrintStrategy.compare(fingerMap.get(fileName),fingerMap.get(comName));
                if(f>90){
                    //相似度高
                    fileList.remove(j);
                    System.out.println(comName);
                    j--;

                File deleteFile =new File(path+"\\"+comName);
                deleteFile.delete();
                }

            }
        }
    }

    @SneakyThrows
    public static boolean compare(){

        String fileA ="C:\\Users\\Administrator\\Desktop\\test\\概述-7 (2).jpg";

        BufferedImage imageA = ImageIO.read( new FileInputStream(fileA) );

        String fileB ="C:\\Users\\Administrator\\Desktop\\test\\概述-10.jpg";

        BufferedImage imageB = ImageIO.read( new FileInputStream(fileB) );


        byte[] bytesA = FingerPrintStrategy.hashValue(imageA);
        byte[] bytesB = FingerPrintStrategy.hashValue(imageB);

        float b =FingerPrintStrategy.compare(bytesA,bytesB);
        System.out.println(b);
        if(b>0.9){
            return true;
        }else{
            return false;
        }
    }





}
